package com.xxx.jdbc;

import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/7/28 14:27
 * @Copyright 博为峰
 */
public class JdbcDemo03 {
    public static void main(String[] args) {
//        query();
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student(100, "岳云鹏2", 36, 170.36, "男", new Date(System.currentTimeMillis()), 2));
        list.add(new Student(100, "孙越", 26, 176.36, "男", new Date(System.currentTimeMillis()), 2));
        insert(list);
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/j13", "root", "123456");
    }

    private static void insert2(ArrayList<Student> list) {
        String sql = "insert into stu(name,age,height,sex,birthday,cla_id)values (?,?,?,?,?,?)";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ) {
            Iterator<Student> iterator = list.iterator();
            while (iterator.hasNext()) {
                Student student = iterator.next();
                preparedStatement.setString(1, student.getName());
                preparedStatement.setInt(2, student.getAge());
                preparedStatement.setDouble(3, student.getHeight());
                preparedStatement.setString(4, student.getSex());
                preparedStatement.setDate(5, student.getBirthday());
                preparedStatement.setInt(6, student.getClaId());
                final int i = preparedStatement.executeUpdate();
                System.out.println(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void insert(ArrayList<Student> list) {
        try (Connection connection = getConnection();
             Statement statement = connection.createStatement();
        ) {
            Iterator<Student> iterator = list.iterator();
            while (iterator.hasNext()) {
                Student student = iterator.next();
                String sql = "insert into stu(name,age,height,sex,birthday,cla_id)values ('" + student.getName() + "'," + student.getAge() + "," + student.getHeight() + ",'" + student.getSex() + "','" + student.getBirthday() + "'," + student.getClaId() + ")";
                int i = statement.executeUpdate(sql);
                System.out.println(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void query() {
        try (Connection connection = getConnection();
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery("select * from stu");) {
            ArrayList<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int anInt = resultSet.getInt(1);
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                double height = resultSet.getDouble("height");
                String sex = resultSet.getString("sex");
                Date birthday = resultSet.getDate("birthday");
                int claId = resultSet.getInt("cla_id");

                Student student = new Student(anInt, name, age, height, sex, birthday, claId);
                list.add(student);
            }
            list.stream().forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void m1() throws SQLException {
        //加载驱动
//        Class.forName("com.mysql.jdbc.Driver");
        //获取数据库连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/j13", "root", "123456");
        //获取执行静态sql语句的Statement对象
        Statement statement = connection.createStatement();
        //执行sql语句
        ResultSet resultSet = statement.executeQuery("select * from stu");
        while (resultSet.next()) {
            int anInt = resultSet.getInt(1);
            System.out.println(anInt);
            String name = resultSet.getString("name");
            System.out.println(name);
        }
    }

}
